<?php

namespace App\Http\Controllers\Web;


use App\Exceptions\MsgException;
use App\Http\Requests\RecruitRequest;
use App\Models\Page;
use App\Repositories\MessageRepository;

use Illuminate\Database\QueryException;


class RecruitController extends BaseController
{
    public function index(){
        $page = Page::where('uri','/recruit')->first();
        return view('web.recruit',compact('page'));
    }

    public function store(RecruitRequest $request,MessageRepository $messageRepository){


        try {

            if(!captcha_check($request->captcha)){
                throw new MsgException("驗證碼錯誤");
            }
            if($messageRepository->groupByDayIpCount() >= 5){
                throw new MsgException("操作次數過多，請稍後再試");
            }
            $messageRepository->store($request->all());
            return $this->success('送出成功','我們會儘快聯係您','/message');
        }catch (MsgException $exception){
            return $this->error('送出失敗',$exception->getMessage(),'/message');
        }catch (QueryException $exception){

            return $this->error('送出失敗','系統出現未知錯誤','/message');
        }catch (\Exception $exception){
            return $this->error('送出失敗','系統出現未知錯誤','/message');
        }



    }
}
